package cn.ulyer.core.secure.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class RedisTokenService  extends AbstractTokenService{

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Override
    public Object store(Object tokenKey,Authentication authentication) {
        redisTemplate.opsForValue().set(String.valueOf(tokenKey),authentication,loginProperties.getTokenExpire(), TimeUnit.HOURS);
        return authentication;
    }

    @Override
    public void remove(String token) {
        redisTemplate.delete(token);
    }

    @Override
    protected Authentication obtainAuthentication(Object tokenKey) {
        Authentication authentication = (Authentication) redisTemplate.opsForValue().get(tokenKey);
        return authentication;
    }

}
